{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " This note book takes the raw records from each biome and different vegetation types. These raw records are aggregated by unique longitude and latitude."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import the necessary packages\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import the raw data from a CSV file. \n",
    "# Here use the file \"grass_flood_raw.csv\" as an example.\n",
    "rsr_raw = pd.read_csv('grass_flood_raw.csv',encoding = 'unicode_escape')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>system.index</th>\n",
       "      <th>index</th>\n",
       "      <th>latitude</th>\n",
       "      <th>longitude</th>\n",
       "      <th>ratio</th>\n",
       "      <th>.geo</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0000000000000000025d</td>\n",
       "      <td>2329</td>\n",
       "      <td>45.396254</td>\n",
       "      <td>133.257157</td>\n",
       "      <td>4.250000</td>\n",
       "      <td>{\"type\":\"Point\",\"coordinates\":[133.25715820250...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0000000000000000025f</td>\n",
       "      <td>2331</td>\n",
       "      <td>43.728557</td>\n",
       "      <td>131.927111</td>\n",
       "      <td>4.250000</td>\n",
       "      <td>{\"type\":\"Point\",\"coordinates\":[131.92711091758...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0000000000000000025b</td>\n",
       "      <td>2327</td>\n",
       "      <td>48.809597</td>\n",
       "      <td>135.508954</td>\n",
       "      <td>4.250000</td>\n",
       "      <td>{\"type\":\"Point\",\"coordinates\":[135.50895518438...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>00000000000000000955</td>\n",
       "      <td>1645</td>\n",
       "      <td>49.092303</td>\n",
       "      <td>130.199800</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>{\"type\":\"Point\",\"coordinates\":[130.19980231981...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>00000000000000000083</td>\n",
       "      <td>1855</td>\n",
       "      <td>48.206068</td>\n",
       "      <td>134.961115</td>\n",
       "      <td>0.817073</td>\n",
       "      <td>{\"type\":\"Point\",\"coordinates\":[134.96111448805...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0          system.index  index   latitude   longitude     ratio  \\\n",
       "0           1  0000000000000000025d   2329  45.396254  133.257157  4.250000   \n",
       "1           2  0000000000000000025f   2331  43.728557  131.927111  4.250000   \n",
       "2           3  0000000000000000025b   2327  48.809597  135.508954  4.250000   \n",
       "3           4  00000000000000000955   1645  49.092303  130.199800  4.000000   \n",
       "4           5  00000000000000000083   1855  48.206068  134.961115  0.817073   \n",
       "\n",
       "                                                .geo  \n",
       "0  {\"type\":\"Point\",\"coordinates\":[133.25715820250...  \n",
       "1  {\"type\":\"Point\",\"coordinates\":[131.92711091758...  \n",
       "2  {\"type\":\"Point\",\"coordinates\":[135.50895518438...  \n",
       "3  {\"type\":\"Point\",\"coordinates\":[130.19980231981...  \n",
       "4  {\"type\":\"Point\",\"coordinates\":[134.96111448805...  "
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rsr_raw.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Remove the nonsignificant columns\n",
    "rsr_agg = rsr_raw.drop(['Unnamed: 0', 'system.index', 'index', '.geo'], axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>latitude</th>\n",
       "      <th>longitude</th>\n",
       "      <th>ratio</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>45.396254</td>\n",
       "      <td>133.257157</td>\n",
       "      <td>4.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>43.728557</td>\n",
       "      <td>131.927111</td>\n",
       "      <td>4.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>48.809597</td>\n",
       "      <td>135.508954</td>\n",
       "      <td>4.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>49.092303</td>\n",
       "      <td>130.199800</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>48.206068</td>\n",
       "      <td>134.961115</td>\n",
       "      <td>0.817073</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    latitude   longitude     ratio\n",
       "0  45.396254  133.257157  4.250000\n",
       "1  43.728557  131.927111  4.250000\n",
       "2  48.809597  135.508954  4.250000\n",
       "3  49.092303  130.199800  4.000000\n",
       "4  48.206068  134.961115  0.817073"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rsr_agg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Group the dataset by longitude and latitude and take the mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [],
   "source": [
    "rsr_aggregated = rsr_agg.groupby(['longitude','latitude']).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>ratio</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>longitude</th>\n",
       "      <th>latitude</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>130.199800</th>\n",
       "      <th>49.092303</th>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>131.927111</th>\n",
       "      <th>43.728557</th>\n",
       "      <td>4.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>133.257157</th>\n",
       "      <th>45.396254</th>\n",
       "      <td>4.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>134.961115</th>\n",
       "      <th>48.206068</th>\n",
       "      <td>0.817073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>135.508954</th>\n",
       "      <th>48.809597</th>\n",
       "      <td>4.250000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         ratio\n",
       "longitude  latitude           \n",
       "130.199800 49.092303  4.000000\n",
       "131.927111 43.728557  4.250000\n",
       "133.257157 45.396254  4.250000\n",
       "134.961115 48.206068  0.817073\n",
       "135.508954 48.809597  4.250000"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rsr_aggregated"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Export the result\n",
    "rsr_aggregated.to_csv('grass_flood_raw_aggregated.csv')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
